Component({
  properties: {
    queId: {
      type: String,
      value: ''
    },
    queTitle: {
      type: String,
      value: '这是个啥，{space}'
    },
    spaceLen: {
      type: Number,
      value: 5
    },
    answer: {
      type: String,
      value: '啥也不是'
    }
  },
  data: {
    // 这里是一些组件内部数据
    title: '填空题',
    spliceQueTitle: [],
    btnText: '下一题',
    loading: false,
    currAnswer: '',
    showTip: false,
    isTrue: false
  },
  attached: function () { 
    //计算标题占位符位置，切割出前后标题
    const _title = this.data.queTitle
    const startSpace = _title.indexOf('{space}')
    const len = 7
    const beforeSapce = _title.slice(0, startSpace)
    let afterSpace = ''
    if (_title.length > (startSpace + len)) {
      afterSpace = _title.slice(startSpace + len)
    } 
    this.setData({
      spliceQueTitle: [beforeSapce, afterSpace]
    })

    //是否到最后一题
    const total = wx.getStorageSync('totalQuestion')
    const currMod = wx.getStorageSync('modNum')
    this.setData({
      btnText: total === (currMod + 1) ? '提交回答' : '下一题'
    })
  },
  methods: {
    // 这里是一个自定义方法
    onStep: function (e) {
      const testMode = wx.getStorageSync('testMode')
      const modNum = wx.getStorageSync('modNum')
      const total = wx.getStorageSync('totalQuestion')

      let _answer = ''
      const objAnswer = e.detail.value
      for (const a of Object.keys(objAnswer)) {
        if (objAnswer[a] === '') {
          wx.showToast({
            title: '请填写答案!',
            icon: 'none',
            duration: 2000
          })
          return
        }
        _answer += objAnswer[a]
      }
      console.log('子健触发', _answer)
      if (testMode === 1) { //填空练习模式
        this.setData({
          showTip: true,
          currAnswer: _answer,
          isTrue: _answer === this.data.answer
        })
      } else if (testMode === 2) {
        this.setData({
          showTip: false,
          currAnswer: _answer
        })
      }
      //考试模式提交答案
      const daAn = wx.getStorageSync('totalAnswer')
      daAn.push({
        id: this.data.queId,
        answer: this.data.currAnswer,
        time_spent: 0
      })
      wx.setStorageSync('totalAnswer', daAn)

      setTimeout(() => {
        this.triggerEvent('myevent', {
          value: modNum + 1,
          complete: total === (modNum + 1)
        }, {})
      }, 1500)
    }
  }
})